package net1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpEchoServer {
    private static final int port = 8888;

    private static final String path = "";

    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(port);
        while (true) {
            System.out.println("-------------------------------");
            System.out.println("等待客户端建立TCP连接...");
            Socket socket = server.accept();
            System.out.println("客户端ip :" + socket.getInetAddress().getHostAddress());
            System.out.println("客户端port :" + socket.getPort());
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String request = bufferedReader.readLine();
            System.out.println("客户端请求的文件路径为：" + path + request);
            File dir = new File(path + request);
            File[] files = dir.listFiles();
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "utf-8"));
            if (files != null) {
                for (File f : files) {
                    writer.println(f.getName());
                }
            }
            writer.flush();
            socket.close();
        }
    }
}
